package com.example.test.hc;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.sun.jna.Pointer;
import lombok.Data;
import java.io.File;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

@Data
public class MyFMSGCallBack implements HCNetSDK.FMSGCallBack_V31 {

    private Stack<Map<String,Object>> stack = new Stack<>();

    @Override
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        AlarmDataHandle(lCommand, pAlarmer, pAlarmInfo);
        return true;
    }

    private void AlarmDataHandle(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pointerAlarmInfo) {
        System.out.printf("command:%d%n", lCommand);
        // 实时人脸抓拍上传
        if (lCommand == HCNetSDK.COMM_UPLOAD_FACESNAP_RESULT) {
            Map<String, Object> data = new HashMap<>();
            // 摄像头信息
            data.put("cameraIp", new String(pAlarmer.sDeviceIP).trim());
            data.put("cameraPort", pAlarmer.wLinkPort);

            HCNetSDK.NET_VCA_FACESNAP_RESULT faceSnapResult = new HCNetSDK.NET_VCA_FACESNAP_RESULT();
            faceSnapResult.write();
            Pointer pointerFaceSnapInfo = faceSnapResult.getPointer();
            pointerFaceSnapInfo.write(0, pointerAlarmInfo.getByteArray(0, faceSnapResult.size()), 0, faceSnapResult.size());
            faceSnapResult.read();
            // 人脸图
            byte[] smallImageBytes = faceSnapResult.pBuffer1.getByteArray(0, faceSnapResult.dwFacePicLen);
            data.put("smallImageBytes", smallImageBytes);
            FileUtil.writeBytes(smallImageBytes, new File("/home/wangbo/temp", DateUtil.formatTime(new Date()) + "_" + "small.jpg"));
            // 背景图
            byte[] bigImageBytes = faceSnapResult.pBuffer2.getByteArray(0, faceSnapResult.dwBackgroundPicLen);
            data.put("bigImageBytes", bigImageBytes);
            FileUtil.writeBytes(bigImageBytes, new File("/home/wangbo/temp",DateUtil.formatTime(new Date()) + "_" + "big.jpg"));
            // 人脸特征
            HCNetSDK.NET_VCA_HUMAN_FEATURE faceFeature = faceSnapResult.struFeature;
            byte age = faceFeature.byAge; // 年龄: 0-表示“未知”（算法不支持）,0xff-算法支持，但是没有识别出来
            byte sex = faceFeature.bySex; // 性别, 0-表示“未知”（算法不支持）,1 – 男 , 2 – 女, 0xff-算法支持，但是没有识别出来
            byte mask = faceFeature.byMask; // 是否戴口罩 0-表示“未知”（算法不支持）,1 – 不戴, 2 – 戴, 0xff-算法支持，但是没有识别出来
            byte eyeGlass = faceFeature.byEyeGlass; // 是否戴眼镜 0-表示“未知”（算法不支持）,1 – 不戴, 2 – 戴,0xff-算法支持，但是没有识别出来
            data.put("age", (int) age);
            data.put("gender", (int) sex);
            data.put("mask", (int) mask);
            data.put("eyeGlass", (int) eyeGlass);
            // 温度数据
            if (faceSnapResult.byAddInfo == 1) {
                HCNetSDK.NET_VCA_FACESNAP_ADDINFO strAddInfo = new HCNetSDK.NET_VCA_FACESNAP_ADDINFO();
                strAddInfo.write();
                Pointer pAddInfo = strAddInfo.getPointer();
                pAddInfo.write(0, faceSnapResult.pAddInfoBuffer.getByteArray(0, strAddInfo.size()), 0, strAddInfo.size());
                strAddInfo.read();

                byte faceSnapThermometryEnabled = strAddInfo.byFaceSnapThermometryEnabled; //人脸抓拍测温使能 1-开启 0-关闭
                byte isAbnomalTemperature = strAddInfo.byIsAbnomalTemperature; //人脸抓拍测温是否温度异常 1-是 0-否
                byte thermometryUnit = strAddInfo.byThermometryUnit; //测温单位: 0-摄氏度（℃），1-华氏度（℉），2-开尔文(K)
                float alarmTemperature = strAddInfo.fAlarmTemperature; // 测温报警警阈值（精确到小数点后1位）
                float faceTemperature = strAddInfo.fFaceTemperature; // 人脸温度（ - 20.0℃~150.0℃，精确到小数点后1位）
                data.put("alarmTemperature", (float)alarmTemperature);
                data.put("faceTemperature", (float)faceTemperature);
            }
            stack.push(data);
        }

        if (lCommand == HCNetSDK.COMM_ISAPI_ALARM) {
            long now = System.currentTimeMillis();
            System.out.println("receive 6009");
            HCNetSDK.NET_DVR_ALARM_ISAPI_INFO structureEventISAPI = new HCNetSDK.NET_DVR_ALARM_ISAPI_INFO();
            structureEventISAPI.write();
            Pointer pEventISAPI = structureEventISAPI.getPointer();
            pEventISAPI.write(0, pointerAlarmInfo.getByteArray(0, structureEventISAPI.size()), 0, structureEventISAPI.size());
            structureEventISAPI.read();


            byte byDataType = structureEventISAPI.byDataType; // 数据格式:0-invalid,1-xml,2-json
            byte picNum = structureEventISAPI.byPicturesNumber; // 图片个数
            String deviceIp = new String(pAlarmer.sDeviceIP).trim(); // 设备ip
            short devicePort = pAlarmer.wLinkPort; // 设备port

            ByteBuffer jsonBuffers = structureEventISAPI.pAlarmData.getByteBuffer(0, structureEventISAPI.dwAlarmDataLen);
            byte[] jsonBytes = new byte[structureEventISAPI.dwAlarmDataLen];
            jsonBuffers.rewind(); // 复位position到0
            jsonBuffers.get(jsonBytes); // 读取buffer数据到byte[]中

            FileUtil.writeBytes(jsonBytes, "/home/wangbo/json/" + now + ".json");

            for (int i = 0; i < picNum; i++) {
                HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA structurePicData = new HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA();
                structurePicData.write();
                Pointer pointerPicData = structurePicData.getPointer();
                pointerPicData.write(0, structureEventISAPI.pPicPackData.getByteArray(i * structurePicData.size(), structurePicData.size()), 0, structurePicData.size());
                structurePicData.read();


                ByteBuffer picByteBuffer = structurePicData.pPicData.getByteBuffer(0, structurePicData.dwPicLen);
                byte[] picBytes = new byte[structurePicData.dwPicLen];
                picByteBuffer.rewind();
                picByteBuffer.get(picBytes);

                FileUtil.writeBytes(picBytes, "/home/wangbo/pic/" + now + "/" + i + ".jpg");
            }
        }
    }
}
